package com.chinanet.utils.pon;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


public class FH_GetFinalResult {
	/**
	 * Get TxPower
	 * @param txt
	 * @return
	 */
	public String returnTxPower(String txt) {
		String[] a = getPatternTxPower(txt.toString()).split("\\s+");
		try {
			return a[17];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}
	
	/**
	 * returnRxPower
	 * @param txt
	 * @return
	 */
	public String returnRxPower(String txt) {
		String line = "\\--------------------------------------------------------------------------------";
		String[] a = GetFinalResult.returnMatcher(line, line, txt).trim().split("\\s+");
		try {
				return a[14];
			} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnOUNstate
	 * @param txt
	 * @return
	 */
	public String returnOUNstate(String txt) {
		String line = "\\--------------------------------------------------------------------------------";
		String[] a =GetFinalResult.returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			return a[9];
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	/**
	 * returnServiceport_Rate
	 * @param txt
	 * @return
	 */
	public String[] returnServiceport_Rate(String txt) {
		String b[]=new String[3];
		String line = "\\--------------------------------------------------------------------------------";
		String[] a = GetFinalResult.returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[14];
			b[1]=a[15];
			b[2]=a[17];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
			b[2]="无数据";
		}
		return b;
	}
	
	/**
	 * returnPtAndIpaddress
	 * @param txt
	 * @return
	 */
	public String[] returnPtAndIpaddress(String txt) {
		String b[]=new String[2];
		String line = "\\--------------------------------------------------------------------------------";
		String[] a =  GetFinalResult.returnMatcher(line, line, txt).trim().split("\\s+");
		try {
			b[0]=a[20];
			b[1]=a[26];
		} catch (Exception e) {
			e.printStackTrace();
			b[0]="无数据";
			b[1]="无数据";
		}
		return b;
	}
	
	/**
	 * returnSWVER
	 * @param txt
	 * @return
	 */
	public String returnSWVER(String txt) {
		String line = "\\--------------------------------------------------------------------------------";
		String[] a = GetFinalResult.returnMatcher(line, line, txt).trim().split("\\s+");
		try {
				return a[23];
			} catch (Exception e) {
			e.printStackTrace();
		}
		return "无数据";
	}

	private String getalarmtxt(String txt){
		String returnBack = "";
		String start="PROBABLE_CAUSE_DESC";
		String line= "\\;";
		returnBack = GetFinalResult.returnMatcher(start, line, txt);
		return returnBack;
	}

	public  Date toDate(String times) {
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		try {
			if(times.length()<18){
				Date date = sdf.parse("1970-01-01 08:00:00");
				return date;
			}else{
				Date date = sdf.parse(times.trim().substring(0, 19));
				return date;
			}
		} catch (ParseException e) {
			e.printStackTrace();
			return new Date();
		}

	}
	/**
	 *
	 * do Alarm things
	 * @param txt
	 * @return
	 */
	public Map<String,Object> getPatterns_ALARM(String txt){

		Map<String,Object> out=new HashMap<String, Object>();
		txt=getalarmtxt(txt);
//		System.out.println(txt);
		int Alarmfalg=0;
		//ONULos	ONU电源掉电
		int ONULoscount=0;
		int ONULosTimeflag=0;
		//ONU离线
		int ONUOutCount=0;
		String[] Pattentext=txt.split("RACK:");
		if(Pattentext.length>0){
			List<List<String>> firstget=new ArrayList<List<String>>();
			int j=0;
			for (String string : Pattentext) {
				if(j==0){
					List<String> one=new ArrayList<String>();
					String [] a=string.split("\t");
					for(int i=1;i<a.length;i++)
						one.add(a[i]);
					firstget.add(one);
				}else{
					List<String> other=new ArrayList<String>();
					String [] a=string.split("\t");
					for (String here : a)
						other.add(here);
					firstget.add(other);
				}
				j++;
			}
//			for(List<String> a:firstget){
//				int i=1;
//				for(String b:a){
//					System.out.println(i+"."+b);
//					i++;
//				}
//			}

			for(int i=0;i<firstget.size()-1;i++){
				int endone=8;
				int endtwo=9;
				if(firstget.get(i).get(0).trim().equals("OLT PON口收无光")){
					for(int z=0;z<8;z++){
						firstget.get(i).add(firstget.get(i+1).get(z));
					}
					//移除
					while (0<endone)
					{
						firstget.get(i+1).remove(0);
						endone--;
					}
				}else{
					for(int z=0;z<9;z++){
						firstget.get(i).add(firstget.get(i+1).get(z));
					}
					//移除
					while (0<endtwo)
					{
						firstget.get(i+1).remove(0);
						endtwo--;
					}
				}


			}


			firstget.remove(firstget.size()-1);


//		for(int i=0;i<firstget.size()-1;i++){
//			int end=9;
//			for(int z=0;z<9;z++){
//				firstget.get(i).add(firstget.get(i+1).get(z));
//				}
//
//				//移除
//				while (0<end)
//                {
//					firstget.get(i+1).remove(0);
//                     end--;
//                }
//		}
//		firstget.remove(firstget.size()-1);


			List<Date> Timearrays=new ArrayList<>();
			for (List<String> list : firstget) {
				if(list.get(0).trim().indexOf("ONU电源掉电")!=-1){
					Timearrays.add(toDate(list.get(7).trim()));
					ONULoscount++;
				}
				else if(list.get(0).trim().indexOf("ONU离线")!=-1){
					ONUOutCount++;
				}

			}
			if(Timearrays.size()>0) {
				Collections.sort(Timearrays);

				Calendar cal = Calendar.getInstance();
				cal.setTime(Timearrays.get(0));
				cal.add(Calendar.SECOND, 1);
				if (cal.getTime().before(Timearrays.get(Timearrays.size() - 1)) || Timearrays.size() < 2) {
					ONULosTimeflag = 0;
				} else {
					ONULosTimeflag = 1;
				}
			}

			if(txt.indexOf("OLT PON口收无光") != -1){
				Alarmfalg=1;
			}
		}
		System.out.println("ONULoscount:"+ONULoscount);
		System.out.println("ONULosTimeflag :"+ONULosTimeflag);
		System.out.println("ONUOutCount"+ONUOutCount);
		System.out.println("Alarmfalg"+Alarmfalg);
		out.put("Alarmfalg", Alarmfalg);
		out.put("ONULoscount", ONULoscount);
		out.put("ONULosTimeflag",ONULosTimeflag );
		out.put("ONUOutCount", ONUOutCount);
		return out;
	}

	
	
	
	private String getPatternTxPower(String txt) {
		String returnBack = "";
		String line = "\\--------------------------------------------------------------------------------";
		returnBack = GetFinalResult.returnMatcher(line, line, txt);
		return returnBack;
	}




}
